<?php
/**
 * Description: 新版模考估分接口
 * User: devforma
 * Date: 2016/11/3
 * Time: 12:36
 */

require __DIR__ . '/includes/utils.php';
class MockGufen implements iController {

    private $isLoggedIn = false, $userId = 0;

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {

        $this->isLoggedIn = validateUser($queryArgs) === 1;
        if ($this->isLoggedIn)
            $this->userId = $queryArgs['user_id'];

        $data = [
            'mock' => $this->getMockInfo(),
            'gufen'=> $this->getGufenInfo()
        ];

        return genOutput(1, $data);
    }

    /**
     * 获取估分
     *
     * @return array|null
     */
    private function getMockInfo() {

        $data = null;
        //模考信息
        $mokao = getOption('newMockManage');
        if (!empty($mokao) && $mokao['available']) {
            $data = [
                'id' => (int)$mokao['papers'],
                'name' => $mokao['name']
            ];
        }

        return $data;
    }

    /**
     * 获取模考
     *
     * @return array|null
     */
    private function getGufenInfo() {
        global $cmsdb;

        $data = null;
        //模考信息
        $gufen = getOption('newGufenManage');
        if (!empty($gufen) && $gufen['available']) {

            $list = $cmsdb->getResults("SELECT `id`, `name` AS paper_name FROM `original_ztk_papers`
            WHERE `id` IN ({$gufen['papers']}) ORDER BY FIELD(`id`, {$gufen['papers']})");

            //获取用户已做估分试卷
            $done = [];
            if ($this->isLoggedIn) {
                $doneList = (array)$cmsdb->getResults("SELECT `id`, `paper_id`, `status` FROM `qb_relationship_user_paper` 
                WHERE `user_id`=:uid AND `psper_id` IN ({$gufen['papers']}) AND `paper_type`=7", [':uid' => $this->userId]);

                $status = array('1' => 'done', '2' => 'undone', '0' => 'fresh');
                foreach($doneList as $dl) {
                    $done[$dl->paper_id] = [
                        'exercise_id' => (int)$dl->id,
                        'status'      => $status[$dl->status]
                    ];
                }
            }

            //获取总做题人数
            $personNums = [];
            $persons = (array)$cmsdb->getResults("SELECT `paper_id`, COUNT(*) AS num FROM `qb_relationship_user_paper`
                WHERE `paper_id` IN ({$gufen['papers']}) AND `paper_type`=8 group by `paper_id`");
            foreach ($persons as $item) {
                $personNums[$item->paper_id] = (int)$item->num;
            }

            $gufenList = [];
            foreach ($list as $l) {
                $gufenList[] = [
                    'id'          => (int)$l->id,
                    'exercise_id' => isset($done[$l->id]) ? $done[$l->id]['exercise_id'] : 0,
                    'name'        => $l->paper_name,
                    'persons_num' => isset($personNums[$l->id]) ? $personNums[$l->id] : 0,
                    'status'      => isset($done[$l->id]) ? $done[$l->id]['status'] : 'fresh'
                ];
            }

            $data = [
                'name'       => $gufen['name'],
                'paper_list' => $gufenList
            ];
        }

        return $data;
    }
}